package  main.java.leetcode.editor.cn;
//2022-06-24 22:57:34
//给定一棵二叉树的根节点 root ，请找出该二叉树中每一层的最大值。 
//
// 
//
// 示例1： 
//
// 
//
// 
//输入: root = [1,3,2,5,3,null,9]
//输出: [1,3,9]
// 
//
// 示例2： 
//
// 
//输入: root = [1,2,3]
//输出: [1,3]
// 
//
// 
//
// 提示： 
//
// 
// 二叉树的节点个数的范围是 [0,10⁴] 
// -2³¹ <= Node.val <= 2³¹ - 1 
// 
//
// 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 249 👎 0

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class FindLargestValueInEachTreeRow {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new FindLargestValueInEachTreeRow().new Solution();
    }
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    List<Integer> ans = new ArrayList<>();
    public List<Integer> largestValues(TreeNode root) {
        dfs(root,0);
        return ans;
    }

    private void dfs(TreeNode root, int height) {
        if (root == null) {
            return;
        }
        if (height == ans.size()) {
            ans.add(root.val);
        } else {
            ans.set(height, Math.max(root.val, ans.get(height)));
        }
        dfs(root.left,height + 1);
        dfs(root.right,height + 1);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
